﻿Public Class PMT
    Implements IPMT

    Private WithEvents _ParametresGeneraux As IPMTParametresGeneraux
    Private _TablesPMTFlux As IPMTTablesflux
    Private _TablesParam As IPMTTablesParam
    Private _TablesActuarielles As IPMTTablesActuarielles
    Private _GenerationTablesPMTParam As IPMTGenerationTablesParam
    Private _GestionArbo As PMTGestionArbo

    Public Sub New(ParamGeneraux As IPMTParametresGeneraux, _
                   TablesPMTFlux As IPMTTablesflux, _
                   Optional TablesPMTParam As IPMTTablesParam = Nothing, _
                   Optional TablesPMTActuarielles As IPMTTablesActuarielles = Nothing, _
                   Optional GestionArbo As PMTGestionArbo = Nothing)
        _ParametresGeneraux = ParamGeneraux
        AddHandler _ParametresGeneraux.AnneeRealisation_Changed, AddressOf OnAnneeMoisRealisationChanged
        AddHandler _ParametresGeneraux.MoisRealisation_Changed, AddressOf OnAnneeMoisRealisationChanged
        _TablesPMTFlux = TablesPMTFlux
        If TablesPMTParam Is Nothing Then
            _TablesParam = New PMTTablesParam
        Else
            _TablesParam = TablesPMTParam
        End If
        If TablesPMTActuarielles Is Nothing Then
            _TablesActuarielles = New PMTTableActuarielle
        Else
            _TablesActuarielles = TablesPMTActuarielles
        End If
        If Not GestionArbo Is Nothing Then
            _GestionArbo = GestionArbo
        End If
        _GenerationTablesPMTParam = Nothing
    End Sub

    Public ReadOnly Property ParametresGeneraux As IPMTParametresGeneraux Implements IPMT.ParametresGeneraux
        Get
            Return _ParametresGeneraux
        End Get
    End Property

    Public ReadOnly Property TablesPMTFlux As IPMTTablesflux Implements IPMT.TablesPMTFlux
        Get
            Return _TablesPMTFlux
        End Get
    End Property

    Public ReadOnly Property TablesParam As IPMTTablesParam Implements IPMT.TablesParam
        Get
            Return _TablesParam
        End Get
    End Property

    Public ReadOnly Property TablesActuarielles As IPMTTablesActuarielles Implements IPMT.TablesActuarielles
        Get
            Return _TablesActuarielles
        End Get
    End Property

    Public ReadOnly Property GenerationTablesPMTParam As IPMTGenerationTablesParam Implements IPMT.GenerationTablesPMTParam
        Get
            Return _GenerationTablesPMTParam
        End Get
    End Property

    Public ReadOnly Property GestionArbo As PMTGestionArbo Implements IPMT.GestionArbo
        Get
            If _GestionArbo Is Nothing Then
                _GestionArbo = New PMTGestionArbo(ParametresGeneraux.Mois)
            End If
            Return _GestionArbo
        End Get
    End Property

    Private Sub OnAnneeMoisRealisationChanged(sender As Object, e As EventArgs)
        'Si le changement concerne la variable Mois des paramètres généraux :
        '    - Mettre à jour les racines des paramètres généraux
        '    - Mettre à jour les FichierPMT des tables du Flux
        '    - Actualiser tout cela

        If ParametresGeneraux.Mois <> "-" Then
            GestionArbo.Mois = ParametresGeneraux.Mois

            ParametresGeneraux.RacineInfocentre = New DirectoryPMT(GestionArbo.NomRacineInfocentre)
            ParametresGeneraux.RacineParametres = New DirectoryPMT(GestionArbo.NomRacineParametres)

            'Paramétrage du mois du dernier inventaire et de l'année du dernier T4
            If ParametresGeneraux.MoisRealisation <= 3 Then
                ParametresGeneraux.AnneeDernierArrete = ParametresGeneraux.AnneeRealisation - 1
            Else
                ParametresGeneraux.AnneeDernierArrete = ParametresGeneraux.AnneeRealisation
            End If
            If ParametresGeneraux.MoisRealisation <= 3 Then
                ParametresGeneraux.MoisDernierArrete = 12
            ElseIf ParametresGeneraux.MoisRealisation <= 6 Then
                ParametresGeneraux.MoisDernierArrete = 3
            ElseIf ParametresGeneraux.MoisRealisation <= 9 Then
                ParametresGeneraux.MoisDernierArrete = 6
            Else
                ParametresGeneraux.MoisDernierArrete = 9
            End If

            'Mise à jour du nom du PMT

            TablesPMTFlux.HistoCRT = New FichierPMT(GestionArbo.NomTbleFluxHistoCRT)
            TablesPMTFlux.HistoPMT = New FichierPMT(GestionArbo.NomTbleFluxHistoPMT)
            TablesPMTFlux.TxComContractuels = New FichierPMT(GestionArbo.NomTbleFluxTxCom)
            TablesPMTFlux.DeroulesCBP = New FichierPMT(GestionArbo.NomTbleFluxDerCBP)
            TablesPMTFlux.SchemaReass = New FichierPMT(GestionArbo.NomTbleFluxReass)

        End If
        'Parcourir les deux paramètres généraux

        'Parcourir tous les FichiersPMT présents dans TablesPMTFlux et recalculer le nouveau nom en fonction du Tag

    End Sub

    Public Function GetSchema() As Xml.Schema.XmlSchema Implements Xml.Serialization.IXmlSerializable.GetSchema
        Return Nothing
    End Function

    Public Sub ReadXml(reader As Xml.XmlReader) Implements Xml.Serialization.IXmlSerializable.ReadXml

    End Sub

    Public Sub WriteXml(writer As Xml.XmlWriter) Implements Xml.Serialization.IXmlSerializable.WriteXml
        writer.WriteStartElement("PMT")

        writer.WriteStartElement("ParametresGeneraux")
        ParametresGeneraux.WriteXml(writer)
        writer.WriteEndElement()

        writer.WriteStartElement("TablesPMTFlux")
        TablesPMTFlux.WriteXml(writer)
        writer.WriteEndElement()

        writer.WriteStartElement("TablesParam")
        TablesParam.WriteXml(writer)
        writer.WriteEndElement()

        writer.WriteStartElement("TablesActuarielles")
        TablesActuarielles.WriteXml(writer)
        writer.WriteEndElement()

        writer.WriteStartElement("GestionArbo")
        GestionArbo.WriteXml(writer)
        writer.WriteEndElement()

        writer.WriteEndElement()
    End Sub
End Class